//
//  SingleHRVProvider.swift
//  DaRings
//
//  Created by 易先生 on 2023/2/18.
//

import UIKit
import MYModule
import HandyJSON

import RxSwift
import RxCocoa

open class SingleHRVProvider: NSObject {

    public var date: Date?
    
    /// 当前 HRV 数据
    public var hrvModel: SingleHRVModel?
    
//    /// 24小时数据
//    var oneDayHRVModels: [SingleHRVModel]?
    
    /// 半小时一个数组一天数据
    public var halfAnHourHRVModels: [[SingleHRVModel]]?
    
    /// 最近7次数据
    public var trend7TimesHRVModels: [SingleHRVModel]?
    
    /// 一天HRV
    public var FullDayHRVModel: HRVModel?
    
//    /// 睡眠情况
//    var hrvSleepConfig = BehaviorRelay<SleepAvgConfig?>(value: nil)
//
    // 半小时一个HRV 图表数据
    open var halfAnHourHRVData: [HRVChartData] {
        
        guard let halfHRVModel = self.halfAnHourHRVModels else {
            return []
        }
        
        let hrvDatas = halfHRVModel.enumerated().compactMap { i, model ->HRVChartData? in
             
             var hrvAvg = 0
             if !model.isEmpty {
                 
                let re = model.reduce(0) { partialResult, singleHRVModel in
                     return partialResult + singleHRVModel.hrv
                 }
                 hrvAvg = re/model.count
             }
            
             let minute: CGFloat = CGFloat(i) * 30.0
             
             if hrvAvg == 0 {
                 return nil
             }
             
             return HRVChartData(hrv: hrvAvg, minute: minute)
             
         }
        
        return hrvDatas
    }
    
  
    public init(date: Date?) {
        super.init()
        
        guard let date = date else {
            return
        }
        
        self.date = date
        
        reloadData()
        
    }
    
    
    public init(hrvModel: SingleHRVModel?) {
        super.init()
        
        self.date = hrvModel?.date
        
        reloadData(hrvModel)
    }
    
    
    open func reloadData(_ hrvModel: SingleHRVModel? = nil) {
        
        guard let date = date else {
            return
        }
        if let hrvModel = hrvModel {
            self.hrvModel = hrvModel
        } else {
            self.hrvModel = SingleHRVModel.last(from: date.timeIntervalSince1970)
        }
        

        self.halfAnHourHRVModels = SingleHRVModel.halfAnHourForDay(from: date)
        
        sevenTimesHRV()
        
        self.FullDayHRVModel = HRVModel.day(date: date)
        
       
        let provider = SleepProvider(date: date)
        let sleepRecord = provider.curDayDetail
        let heartList = provider.hrvData
        
//        let config = provider.configModel(sleepRecord, list: heartList)
//        if var config = config {
//            config.low = config.low == 255 ? 0 : config.low
//        }
//        self.hrvSleepConfig.accept(config)
//

    }
    
    
    open func sevenTimesHRV() {
        var trend7TimesHRVModels = [SingleHRVModel]()
        if let hrvModel = hrvModel {
            trend7TimesHRVModels = SingleHRVModel.sevenTimesModel(from: hrvModel.end)
            
        }
        if trend7TimesHRVModels.count < 7 {
            
            let emptyData = (trend7TimesHRVModels.count ..< 7).map { _ in SingleHRVModel() }
            trend7TimesHRVModels.append(contentsOf: emptyData)
            
        }
        
        self.trend7TimesHRVModels = trend7TimesHRVModels.reversed()
    }
    
    open var toDayDate: Date? {
        
        guard let hrvModel = hrvModel else {
            return nil
        }
        let date = Date(timeIntervalSince1970: hrvModel.end)
        
        return date
    }
    
}
